package com.yhb.sign;

import com.yhb.exception.InvalidEncryptedKeyException;
import org.apache.commons.lang.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * @Title: AESUtil
 * @Copyright: Copyright (c) 2019
 * @Description: <br> 数据内容加解密工具类
 */
public class AESUtil {

    private static final String ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * aes解密-256位
     */
    public static String aesDecrypt(String encryptContent, String password) throws InvalidEncryptedKeyException {
        if (StringUtils.isBlank(password)) {
            throw new InvalidEncryptedKeyException("password is not null");
        }
        if (password.length() !=16) {
            throw new InvalidEncryptedKeyException("password must be is 16 bytes");
        }
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            keyGen.init(128, secureRandom);
            SecretKey secretKey = keyGen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.decode(encryptContent)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * aes加密-256位
     */
    public static String aesEncrypt(String content, String password) throws InvalidEncryptedKeyException {
        if (StringUtils.isBlank(password)) {
            throw new InvalidEncryptedKeyException("password is not null");
        }
        if (password.length() !=16) {
            throw new InvalidEncryptedKeyException("password must be is 16 bytes");
        }
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            keyGen.init(128, secureRandom);
            SecretKey secretKey = keyGen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return Base64.encode(cipher.doFinal(content.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String  encryptBeforeContent="你好啊，张三";
        System.out.println("AES128加密之前内容是:"+encryptBeforeContent);
        String password = EncryptedRandomKey.getEncryptedRandomKey();
        System.out.println("AES128加密密钥是:"+password);
        try {
            String encryptAfterContent = AESUtil.aesEncrypt(encryptBeforeContent,password);
            System.out.println("AES128加密之后内容是:"+encryptAfterContent);
            System.out.println("AES128解密之后内容是"+AESUtil.aesDecrypt(encryptAfterContent,password));
        } catch (InvalidEncryptedKeyException e) {
            e.printStackTrace();
        }
    }
}
